<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2126 – Consider using `grep -c` instead of `grep | wc`</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2126 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2126">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2 id="consider-using-grep--c-instead-of-grep--wc">Consider using
<code>grep -c</code> instead of <code>grep | wc</code></h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2126.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">grep</span> foo <span class="kw">|</span> <span class="fu">wc</span> <span class="at">-l</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2126.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">grep</span> <span class="at">-c</span> foo</span></code></pre></div>
<h4 id="for-multiple-files">For multiple files</h4>
<p>Instead of:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="SC2126.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">grep</span> foo <span class="pp">*</span>.log <span class="kw">|</span> <span class="fu">wc</span> <span class="at">-l</span></span></code></pre></div>
<p>You can pipe all the file contents into <code>grep</code> (passing
the files directly to <code>grep</code> causes <code>-c</code> to print
each file's count separately, rather than the total):</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb4-1"><a href="SC2126.html#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span> <span class="pp">*</span>.log <span class="kw">|</span> <span class="fu">grep</span> foo <span class="at">-c</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>This is purely a stylistic issue. <code>grep</code> can count lines
without piping to <code>wc</code>.</p>
<p>Often this number is only used to see whether there are matches (i.e.
<code>== 0</code>). In these cases it's clearer and more efficient to
use <code>grep -q</code> and check its exit status:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb5-1"><a href="SC2126.html#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="fu">grep</span> <span class="at">-q</span> pattern file<span class="kw">;</span> <span class="cf">then</span></span>
<span id="cb5-2"><a href="SC2126.html#cb5-2" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> <span class="st">&quot;The file contains the pattern&quot;</span></span>
<span id="cb5-3"><a href="SC2126.html#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="cf">fi</span></span></code></pre></div>
<p>Also note that in <code>foo | grep bar | wc -l</code>,
<code>wc</code> will mask the exit code of <code>grep</code> by default
(i.e. without <code>set -o pipefail</code>), and always return success.
If replacing with <code>foo | grep -c bar</code>, <code>grep</code> will
exit non-zero when there are no matches. This is generally desirable
(see above), but may require handling when used with
<code>set -e</code>.</p>
<h3 id="exceptions">Exceptions</h3>
<p>If you find piping to <code>wc</code> is clearer in a given situation
it's fine to <a href="ignore.html">ignore</a> this error.</p>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


